fix(clipboard): Clear setTimeout in useCopyToClipboard to resolve memory leak #4228
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR for Clipboard Hook Leak (clipboard_44)
This PR addresses the memory leak identified in
leak-report/guaranteed/clipboard_44.md.Description
The
useCopyToClipboardhook inwebview-ui/src/utils/clipboard.tsuses asetTimeoutto manage feedback visibility. This timeout is not cleared when the component unmounts, leading to potential React state update warnings on unmounted components.This patch implements the suggested fix from the leak report, using
useRefto store the timeout ID anduseEffectto ensure the timeout is cleared on component unmount or whenfeedbackDurationchanges.Related Bug Report
#4227
Changes
webview-ui/src/utils/clipboard.tsto correctly manage thesetTimeoutlifecycle.useEffectanduseRef.clearTimeoutis called on unmount and before setting a new timeout.useCopyToClipboard.How to Test
useCopyToClipboardhook into a test component.copyWithFeedback.feedbackDuration(e.g., 2 seconds) expires.Important
Fixes memory leak in
useCopyToClipboardby managingsetTimeoutlifecycle withuseRefanduseEffect.useCopyToClipboardinclipboard.tsby managingsetTimeoutlifecycle.useRefto store timeout ID anduseEffectto clear timeout on unmount orfeedbackDurationchange.copyWithFeedbackto clear existing timeout before setting a new one.useEffectto clear timeout on component unmount.useCopyToClipboard.This description was created by
for a9b66bb. You can customize this summary. It will automatically update as commits are pushed.